package com.easychat.utils.controller;

import com.easychat.utils.dao.UtilMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author kyle.li
 * @version 1.0
 * @description: TODO
 * @date 2024/7/2 18:51
 */
@RestController
@RequestMapping("/utils")
public class UtilController {
    @Resource
    UtilMapper utilMapper;
//    @RequestMapping("/toGettype")
//    public String toGetTypePage(){
//        return "getType";
//    }

    @RequestMapping(value = "/generate")
    public String getType(String database, String table){

        Map map = new HashMap<>();

        map.put("database",database);
        //数据库的表名字
        List list = new ArrayList();
//        String[] tables_arr = tables.split(",");

//        for (String name : tables_arr){
//            list.add(name);
//        }
        map.put("table",table);

        List result =utilMapper.getType(map);
        Map NameAndType = new HashMap();

        for(Object item : result){
            HashMap hashMap = (HashMap) item;
            String  columnName = (String) hashMap.get("column_name");
            String dataType = ((String) hashMap.get("data_type")).toUpperCase();
            NameAndType.put(columnName,dataType);
        }

        StringBuilder stringBuilder =new StringBuilder();

        System.out.println("---------------生成Result 字符串----------------------");
        Iterator<String>  iterator = NameAndType.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String type = (String) NameAndType.get(key);
            String template ="<result column="+'\"'+key+'\"'+" jdbcType="+'\"'+type+'\"'+ " property="+'\"'+ replaceUnderlineAndfirstToUpper(key,"_","")+'\"'+"/>";
            System.out.println(template);
            stringBuilder.append(template);
            stringBuilder.append(System.getProperty("line.separator"));
        }
        System.out.println("---------------生成Entity字段----------------------");
        Iterator <String> iterator2 = NameAndType.keySet().iterator();
        while (iterator2.hasNext()) {
            String key = iterator2.next();
            String type = (String) NameAndType.get(key);

            String template ="private "+ MysqlToJava(type) + " " + replaceUnderlineAndfirstToUpper(key,"_","")+";";
            System.out.println(template);
            stringBuilder.append(template);
            stringBuilder.append(System.getProperty("line.separator"));
        }

        return  stringBuilder.toString();
    }
    //把mysql类型转成java类型
    public static String MysqlToJava(String type){
        String result="";
        switch(type) {
            case "INT":
                result = "int";
                break;
            case "TINYINT":
                result = "int";
                break;

            case "VARCHAR":
                result = "String";
                break;
            case "CHAR":
                result = "String";
                break;

            case "DATETIME":
                result = "Date";
                break;
            case "TIME":
                result = "Time";
                break;
            case "BIT":
                result = "Boolean";
                break;
        }
        return result;


    }

    /**
     * 首字母大写
     *
     * @param srcStr
     * @return
     */
    public static String firstCharacterToUpper(String srcStr) {
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
    }
    /**
     * 替换字符串并让它的下一个字母为大写 例如: create_time ---> createTime
     * @param srcStr
     * @param org
     * @param ob
     * @return
     */
    public static String replaceUnderlineAndfirstToUpper(String srcStr,String org,String ob)
    {
        String newString = "";
        int first=0;
        while(srcStr.indexOf(org)!=-1)
        {
            first=srcStr.indexOf(org);
            if(first!=srcStr.length())
            {
                newString=newString+srcStr.substring(0,first)+ob;
                srcStr=srcStr.substring(first+org.length(),srcStr.length());
                srcStr=firstCharacterToUpper(srcStr);
            }
        }
        newString=newString+srcStr;
        return newString;
    }
}
